//
//  YHCashBackRuleSubViewController.m
//  YHApp
//
//  Created by -- on 2021/12/21.
//

#import "HACashBackRuleSubVC.h"
#import "HACanalItemMo.h"
#import "HAPolicyMo.h"
#import "HAChoicePolicyModPopView.h"
#import "HAPolicyActMo.h"
#import "HAHoemAgentMakeCashBackCell.h"

@interface HACashBackRuleSubVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *dataArray;
/***/
@property (nonatomic, strong) NSMutableArray *policyArray;
/***/
@property (nonatomic, strong) NSMutableArray *itemArray;
/***/
@property (nonatomic, strong) HAPolicyMo *selPolicyMo;

@end

@implementation HACashBackRuleSubVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    [self loadData];

    // Do any additional setup after loading the view.
}

-(void)loadData {
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"policy/items";
    request.parms = @{
        @"source_id":self.canalItemModel.value?:@"",
    };
    [MBProgressHUD showLoading:self.view];
    
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {
            
            [MBProgressHUD hideHUDForView:self.view];
            
            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                if ([dataModel.data objectForKey:@"data"]) {
                    NSArray *arr = [HAPolicyMo mj_objectArrayWithKeyValuesArray:dataModel.data[@"data"]];
                    [self.policyArray removeAllObjects];
                    [self.policyArray addObjectsFromArray:arr];
             
                    [self.dataArray removeAllObjects];
                    [self.dataArray addObject:@"返利政策"];
                    [self.tableView reloadData];

                }
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
            [self.tableView reloadData];
        }
    }];
}

-(void)loadModData:(HAPolicyMo *)model {
    SYRequest *request = [[SYRequest alloc]init];
    request.requestMethod = SYRequestMethodGET;
    request.requestUrl = @"reward/rebate.html";
    request.parms = @{
        @"source_id":self.canalItemModel.value?:@"",
        @"policy_id":model.value?:@"",
    };
    [MBProgressHUD showLoading:self.view];
    [[SYNetworking shareSingle] hgj_asynRequestWithArray:@[request] responseBlock:^(SYDataModel *dataModel) {
        if (dataModel.request == request) {

            [MBProgressHUD hideHUDForView:self.view];

            if (dataModel.err) {
                [MBProgressHUD showError:@"网络错误"];
                return;
            }
            if (dataModel.code == 1) {
                if ([[dataModel.data allKeys]containsObject:@"items"]) {
                    NSArray *arr = [HAPolicyActMo mj_objectArrayWithKeyValuesArray:[dataModel.data valueForKey:@"items"]];
                    [self.itemArray removeAllObjects];
                    [self.itemArray addObjectsFromArray:arr];
                    if (self.itemArray.count > 0) {
                        [self.dataArray removeAllObjects];
                        [self.dataArray addObject:@"返利政策"];
                        [self.dataArray addObject:@"内容"];
                        [self.tableView reloadData];
                    }
                }
            }else{
                [MBProgressHUD showError:dataModel.msg];
            }
            [self.tableView reloadData];
        }
    }];
}

-(void)setupUI {

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"返利政策"]) {
        return 56;
    }
    if ([title isEqualToString:@"内容"]) {
        CGFloat h = 0;
        for (HAPolicyActMo *itemMo in self.itemArray) {
            h += 54;
            h += itemMo.items.count * (54+36);
        }
        return h;
    }
    return 0;
}


- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"内容"]) {
        HAHoemAgentMakeCashBackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHHoemAgentMakeCashBackCellID"];
        if (!cell) {
            cell = [[HAHoemAgentMakeCashBackCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHHoemAgentMakeCashBackCellID"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.isNotEeit = YES;
        cell.itemArray = self.itemArray;
        return cell;
    }else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YHMeSettlementMakeMitCellID"];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YHMeSettlementMakeMitCellID"];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if ([title isEqualToString:@"返利政策"]) {
            [self setCellMod:cell.contentView];
        }
        return cell;
    }
    return [UITableViewCell new];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WS(weakSelf)
    NSString *title = self.dataArray[indexPath.row];
    if ([title isEqualToString:@"返利政策"]) {
        HAChoicePolicyModPopView *menu = [[HAChoicePolicyModPopView alloc]initWithArray:self.policyArray];
        [menu show];
        menu.YHChoicePolicyModMenuSureBlock = ^(HAPolicyMo * _Nonnull model) {
            weakSelf.selPolicyMo = model;
            [weakSelf loadModData:model];
        };
    }
}

-(void)setCellMod:(UIView *)contentView {
    for (UIView *view in contentView.subviews) {
        [view removeFromSuperview];
    }
    contentView.backgroundColor = [UIColor khaBGColor];

    UIView *mainV = [UIView ha_createView];
    mainV.backgroundColor = [UIColor whiteColor];
    [contentView addSubview:mainV];
    [mainV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.top.mas_equalTo(2);
        make.bottom.mas_equalTo(0);
    }];

    UILabel *titlabel = [UILabel ha_NLabelWithFont:17 textColor:@"#000000" text:@"返利政策" alignment:0];
    [mainV addSubview:titlabel];
    [titlabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(20);
        make.width.mas_offset(100);
    }];

    UIImageView *RImgV = [UIImageView ha_createImageView];
    RImgV.image = [UIImage imageNamed:@"ic_cell_more"];
    [mainV addSubview:RImgV];
    [RImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_offset(5);
        make.height.mas_offset(10);
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-20);
    }];


    UILabel *conLabel = [UILabel ha_NLabelWithFont:15 textColor:@"#858b9c" text:@"请选择" alignment:2];
    [contentView addSubview:conLabel];
    [conLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.left.mas_equalTo(titlabel.mas_right).offset(15);
        make.right.mas_equalTo(-10-20-10);
    }];

    if (self.selPolicyMo) {
        conLabel.text = self.selPolicyMo.text;
        conLabel.textColor = [UIColor colorWithHexString:@"#000000"];
    }else {
        conLabel.text = @"请选择";
        conLabel.textColor = [UIColor colorWithHexString:@"#858b9c"];
    }
}


- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        // 去掉滚动条
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor khaBGColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}


- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (NSMutableArray *)policyArray
{
    if (!_policyArray) {
        _policyArray = [NSMutableArray array];
    }
    return _policyArray;
}

- (NSMutableArray *)itemArray
{
    if (!_itemArray) {
        _itemArray = [NSMutableArray array];
    }
    return _itemArray;
}

@end
